#include <stdio.h>
#include <stdlib.h>
void shellsort(int data[], int n)
{
    int *delta, k, i = 0, t, dk, j;
    k = n;
    delta = (int *)malloc(sizeof(int) * (n / 2));
    if (i == 0)
        do
        {
            k /= 2;
            delta[i++] = k;
        } while (k != 1);
    i = 0;
    while ((dk = delta[i]) > 0)
    {
        for (k = delta[i]; k < n; ++k)
            if (k > 0)
            {
                t = data[k];
                for (j = k - dk; j >= 0 && t < data[j]; j -= dk)
                {
                    data[j + dk] = data[j];
                } /*for*/
                data[j + dk] = t;
            } /*if*/
        printf("第%d次排序:\n",i+1);
        for (int i = 0; i < 7; i++)
        {
            printf("%d ", data[i]);
        }
        printf("\n");
        ++i;
    } /*while*/
}
int main()
{
    int d[20] = {15, 9, 7, 8, 20, -1, 4};
    shellsort(d, 7);
    printf("希尔排序结果:\n");
    for (int i = 0; i < 7; i++)
    {
        printf("%d ", d[i]);
    }
    printf("\n");
    return 0;
}